package com.cskaoyan.exception;

import com.cskaoyan.bean.BaseRespVo;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authz.AuthorizationException;
import com.cskaoyan.utils.ErrState;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionControllerAdvice {

    // Admin数据校验异常
    @ExceptionHandler(MyValidationException.class)
    public BaseRespVo valid(MyValidationException exception) {
        String message = exception.getMessage();
        return BaseRespVo.fail(message);
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public BaseRespVo paramError(){
        return BaseRespVo.fail(ErrState.PARAM_ERROR.getErrno(),ErrState.PARAM_ERROR.getErrmsg());
    }

    //未登录异常
    @ExceptionHandler(UnLoginException.class)
    public BaseRespVo valid(UnLoginException exception) {
        String message = exception.getMessage();
        return BaseRespVo.fail(501,message);
    }


    // Shiro 认证异常
    @ExceptionHandler(IncorrectCredentialsException.class)
    public BaseRespVo incorrectAuthen(IncorrectCredentialsException exception) {
        return BaseRespVo.fail("用户名或密码错误！");
    }

    // Shiro 授权异常
    @ExceptionHandler(AuthorizationException.class)
    public BaseRespVo incorrectAuthor(AuthorizationException exception) {
        return BaseRespVo.fail("当前用户没有该权限！");
    }



}
